package fr.enseirb.mycub.structures.tramway;

import java.util.ArrayList;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import fr.enseirb.mycub.db.TramwayDBConstants;
import fr.enseirb.mycub.structures.Line;
import fr.enseirb.mycub.structures.SpecificLine;
import fr.enseirb.mycub.structures.Station;

/**
 * Model class which will represent a tramway station
 * 
 * @author jletemple, coudar
 * 
 */

public class TramwayStation extends Station {
	private ArrayList<SpecificLine> line;

	public static final String LINE = "lineNumber";

	public TramwayStation(int id, String name,
			int longitude, int latitude, ArrayList<SpecificLine> line,
			boolean isOpen, boolean isFavorite, int distance) {
		super();
		super.mId = id;
		super.mName = name;
		setGeoPoint(new GeoPoint(latitude, longitude));
		this.setLine(line);
		super.mIsOpen = isOpen;
		super.mIsFavorite = isFavorite;
		super.mDistance = distance;
	}

	public TramwayStation(){}

	public ArrayList<SpecificLine> getLine() {
		return line;
	}

	public void setLine(ArrayList<SpecificLine> line) {
		this.line = line;
	}

	@Override
	public String toString() {
		return "TramwayStation [line=" + line + ", mId=" + mId + ", mName="
				+ mName + ", mGeoPoint=" + mGeoPoint + ", mDistance="
				+ mDistance + ", mIsOpen=" + mIsOpen + ", mIsFavorite="
				+ mIsFavorite + "]";
	}

	public static ArrayList<TramwayStation> getSimpleStationFromCursor(ArrayList<SpecificLine> specificLines,
			Cursor stationsCursor, Cursor linkTableCursor){

		ArrayList<TramwayStation> tramStations = new ArrayList<TramwayStation>();

		if(stationsCursor.getCount() == 0 ) return tramStations;

		//------- stations table -------

		int latitudeColumn = stationsCursor
				.getColumnIndex(TramwayDBConstants.KEY_LATITUDE);
		int longitudeColumn = stationsCursor
				.getColumnIndex(TramwayDBConstants.KEY_LONGITUDE);
		int idColumn = stationsCursor
				.getColumnIndex(BaseColumns._ID);

		//------- link table -------

		int specificLineColumn = linkTableCursor.getColumnIndex(TramwayDBConstants.KEY_SPECIFIC_LINE_ID);
		int stationColumn = linkTableCursor.getColumnIndex(TramwayDBConstants.KEY_STATION_ID);

		stationsCursor.moveToFirst();
		TramwayStation l = null;

		int size = stationsCursor.getCount();

		for(int i =  0; i < size; i ++){
			l = new TramwayStation();
			l.mId = stationsCursor.getInt(idColumn);
			int lat = stationsCursor.getInt(latitudeColumn);
			int lon = stationsCursor.getInt(longitudeColumn);
			l.mGeoPoint = new GeoPoint(lat,lon);
			l.setLine(new ArrayList<SpecificLine>());
			//			int id_specificLigne = linkTableCursor.getInt();



			//-----------------------------------------------
			// Boucle sur la table de linkage
			int size2 = linkTableCursor.getCount();
			linkTableCursor.moveToFirst();
			for(int j = 0; j < size2; j ++){
				if(linkTableCursor.getInt(stationColumn) == l.mId){

					//On ajoute ses lignes :
					for(int k = 0;  k < specificLines.size(); k++){
						if(specificLines.get(k).id == linkTableCursor.getInt(specificLineColumn)){
							l.getLine().add(specificLines.get(k));
						}
					}
				}
				
				linkTableCursor.moveToNext();
			}
//			Log.i("TS" , "--> nb de lignes : "+l.getLine().size()+" pour la station : "+l.mId);
			tramStations.add(l);
			stationsCursor.moveToNext();
		}

		stationsCursor.close();

		return tramStations;
	}



}